#include <QDebug>
#include <QFile>
#include "ConsoleWrapper.h"

ConsoleWrapper::ConsoleWrapper(QString program, QObject *parent) :
	QObject(parent)
{
	m_proc.setProgram(program);

	connect(&m_proc, SIGNAL(started()), SIGNAL(SignalProcessStart()));
	connect(&m_proc, SIGNAL(error(QProcess::ProcessError)), SLOT(SlotError(QProcess::ProcessError)));
	connect(&m_proc, SIGNAL(finished(int,QProcess::ExitStatus)), SIGNAL(SignalProcessEnd(int,QProcess::ExitStatus)));
}

void ConsoleWrapper::sendMessage(uint iMsgID, int wParam, int lParam)
{
	qDebug() << "[request send message]" << iMsgID;
	if(m_proc.state() == QProcess::Running)
	{
		qDebug() << "[send result]" << PostMessage(HWND_BROADCAST, iMsgID, (WPARAM)wParam, (LPARAM)lParam);
	}
}

void ConsoleWrapper::SlotStart()
{
	qDebug("[start request]");
	if(m_proc.state() != QProcess::Running && QFile::exists(m_proc.program()))
	{
		qDebug("[started]");
		m_proc.start();
	}
}

void ConsoleWrapper::SlotProcessEnd(int i, QProcess::ExitStatus status)
{
	qDebug() << "[finished]" << i << status;
}

void ConsoleWrapper::SlotError(QProcess::ProcessError error)
{
	qDebug() << "[error]" << error;
}
